package org.hepeng.workx.mybatis.builder.annotation;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
import org.hepeng.workx.extension.XLoader;
import org.hepeng.workx.mybatis.MybatisConstant;
import org.hepeng.workx.mybatis.mapper.MappedStatementSupplier;
import org.hepeng.workx.mybatis.mapper.MapperSupport;
import org.hepeng.workx.util.ObjectProcessor;

import java.util.Objects;

/**
 * @author he peng
 */
public class MapperAnnotationBuilderParseObjectProcessor implements ObjectProcessor<MapperAnnotationBuilderParseObjectProcessor.ParsePreProcessObject, Object> {

    protected static final MappedStatementSupplier MAPPED_STATEMENT_SUPPLIER = XLoader.getXLoader(MappedStatementSupplier.class , MybatisConstant.MYBATIS_X_POINT_DIRECTORY).getX();

    @Override
    public ParsePreProcessObject preProcess(ParsePreProcessObject preProcessObject) {
        Class<?> type = preProcessObject.getType();
        MapperSupport mapperSupport = type.getAnnotation(MapperSupport.class);
        if (Objects.nonNull(mapperSupport)) {
            MAPPED_STATEMENT_SUPPLIER.supply(preProcessObject.getAssistant() , type);
        }
        return preProcessObject;
    }

    @Override
    public Object postProcess(Object postProcessObject) {
        return postProcessObject;
    }

    @Builder
    @Setter
    @Getter
    public static class ParsePreProcessObject {
        private final Configuration configuration;
        private final MapperBuilderAssistant assistant;
        private final Class<?> type;
    }
}
